package com.aaa.sys.service.impl;

import com.aaa.sys.entity.LoginLog;
import com.aaa.sys.mapper.LoginLogMapper;
import com.aaa.sys.service.LoginLogService;
import com.aaa.sys.vo.LoginLogResult;
import com.aaa.sys.vo.LoginLogVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 登录记录 服务实现类
 * </p>
 *
 * @author ykq
 * @since 2020-12-08
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements LoginLogService {

    @Autowired
    private LoginLogMapper loginLogMap;
    @Override
    public IPage<LoginLogResult> findByCondition(Integer current, Integer limit, LoginLogVo loginLogVo) {
        IPage<LoginLogResult> page=new Page<>(current,limit);
        String startTime = loginLogVo.getStartTime();
        String endTime = loginLogVo.getEndTime();
        String logName = loginLogVo.getLogName();
        QueryWrapper<LoginLogResult> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(endTime)){
            queryWrapper.le("log.create_time",endTime.substring(0,10));
        }

        if(StringUtils.isNotEmpty(startTime)){
            queryWrapper.ge("log.create_time",startTime.substring(0,10));
        }
        if(StringUtils.isNotEmpty(logName)){
            queryWrapper.like("log_name",logName);
        }

        return this.loginLogMap.findByCondition(page,queryWrapper);
    }

    @Override
    public void clearLoginLog() {
        this.loginLogMap.clearLoginLog();
    }
}
